package com.movisol.questionwizard.views.asynctasks;

import java.util.List;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.movisol.questionwizard.applicationcontroller.ApplicationController;
import com.movisol.tools.HelperUtils;

public class AsyncSubscribePushNotification extends AsyncTask<String, Void, String> {

	private ApplicationController ac = ApplicationController.getInstance();

	@Override
	protected String doInBackground(String... params) {
		String sku = HelperUtils.getConfigParam("SKU", ac.getContext());
		String version = HelperUtils.getApplicationVersion(ac.getContext());
		String languageId = HelperUtils.getDeviceCountry();
		String userId = HelperUtils.getDeviceId(ac.getContext());
		return ac.subscribePushNotification(sku, version, languageId, userId,"4" /*ANDROID_MEDIUM*/, params[0]);
	}

	/*
	 * (non-Javadoc)
	 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
	 */
	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);
		List<Object> list = HelperUtils.parsePushNotificationResponseToBoolean(result);
		if (list.get(0) != null)
		{
			ac.firePushNotificationSubscribedEvent(null);
		} else
		{
			Log.w(getClass().getName(), "Error al parsear la respuesta del servidor");
			HelperUtils.showToastMessage(ac.getContext(), "Error en el servicio C2DM", Toast.LENGTH_LONG);
//			HelperUtils.LogE(getClass(), SaveUserProfileErrorCodes
//					.getErrorByCode(((ProfileServiceWebFault) list.get(1)).getIntegerErrorCode()));
//			HelperUtils.showToastMessage(pc.getContext(), SaveUserProfileErrorCodes
//					.getErrorByCode(((ProfileServiceWebFault) list.get(1)).getIntegerErrorCode()),
//					Toast.LENGTH_LONG);
		}
	}
}
